package com.ms.teapuesto.util;

/**
 * Created by RAZANERO on 26/08/14.
 */

import com.gargoylesoftware.htmlunit.html.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * User: gmc
 * Date: 07/09/2010
 */
public class HtmlUtil {

    static DecimalFormat df = new DecimalFormat("#00.00");

    public static String bold(String page) {
        return "<span style='color:red'><b>" + page + "</b></span>";
    }

    public static String bold(BigDecimal local, BigDecimal empate, BigDecimal visita) {
        StringBuilder sb = new StringBuilder();

        if (local.doubleValue() >= 50) {
            sb.append(HtmlUtil.bold(df.format(local) + "%") + " ; ");
        } else {
            sb.append(df.format(local) + "% ; ");
        }

        if (empate.doubleValue() >= 50) {
            sb.append(HtmlUtil.bold(df.format(empate) + "%") + " ; ");
        } else {
            sb.append(df.format(empate) + "% ; ");
        }

        if (visita.doubleValue() >= 50) {
            sb.append(HtmlUtil.bold(df.format(visita) + "%") + "  ");
        } else {
            sb.append(df.format(visita) + "%  ");
        }

        return sb.toString();
    }

    public static String bold(BigDecimal local) {
        StringBuilder sb = new StringBuilder();

        if (local.doubleValue() >= 50) {
            sb.append(HtmlUtil.bold(df.format(local) + "%"));
        } else {
            sb.append(df.format(local) + "%");
        }
        return sb.toString();
    }

    public static List<HtmlTable> getHtmlTableClass(HtmlPage page, String styleClass) throws Exception {

        List<HtmlTable> result = new ArrayList<HtmlTable>();
        Iterator<HtmlElement> iterator = page.getHtmlElementDescendants().iterator();
        int counter = 0;
        while (iterator.hasNext()) {
            HtmlElement htmlElement = iterator.next();
            if (htmlElement instanceof HtmlTable) {
                if (htmlElement.getAttributeNode("class")!= null
                        && htmlElement.getAttributeNode("class").getValue().equals(styleClass)) {
                    result.add((HtmlTable) htmlElement);
                }
                counter++;
            }
        }
        return result;
    }

    public static HtmlTable getHtmlTable(HtmlPage page, int tableIdx) throws Exception {

        Iterator<HtmlElement> iterator = page.getHtmlElementDescendants().iterator();
        int counter = 0;
        while (iterator.hasNext()) {
            HtmlElement htmlElement = iterator.next();
            if (htmlElement instanceof HtmlTable) {
                if (counter == tableIdx) {
                    return (HtmlTable) htmlElement;
                }
                counter++;
            }
        }
        throw new Exception("There is not table for index:" + tableIdx);
    }

    public static HtmlInput getHtmlInput(HtmlPage page, String id) throws Exception {

        Iterator<HtmlElement> iterator = page.getHtmlElementDescendants().iterator();
        int counter = 0;
        while (iterator.hasNext()) {
            HtmlElement htmlElement = iterator.next();
            if (htmlElement instanceof HtmlInput) {
                if (id.equals(htmlElement.getId())) {
                    return (HtmlInput) htmlElement;
                }

            }
        }
        throw new Exception("There is not HtmlInput for index:" + id);
    }

    public static HtmlDivision getHtmlDivWithClass(HtmlPage page, String styleClass, int index) throws Exception {

        Iterator<HtmlElement> iterator = page.getHtmlElementDescendants().iterator();
        int counter = 0;
        while (iterator.hasNext()) {
            HtmlElement htmlElement = iterator.next();
            if (htmlElement instanceof HtmlDivision) {
                if (StringUtils.contains(htmlElement.getAttribute("class"), styleClass)) {
                    if (counter == index) {
                        return (HtmlDivision) htmlElement;
                    }
                    counter++;
                }
            }
        }
        throw new Exception("There is not div for id:" + index);
    }


    private static int getTableNumRows(String html) {
        int trIdx = 0;
        int counter = 0;
        while (trIdx != -1) {
            trIdx = html.indexOf("<tr", trIdx + 1);
            counter++;
        }
        return counter - 1;
    }


    private static String getTableFrom(String htmlPage, int tableIndex) {
        int tableIdx = 0;
        for (int i = 0; i < tableIndex + 1; i++) {
            tableIdx = htmlPage.indexOf("<table", tableIdx + 1);
        }
        int tableFinIdx = htmlPage.indexOf("</table", tableIdx);
        return htmlPage.substring(tableIdx, tableFinIdx);
    }

    private static String getTableRowFrom(String html, int rowIndex) {
        int trIdx = 0;
        for (int i = 0; i < rowIndex + 1; i++) {
            trIdx = html.indexOf("<tr", trIdx + 1);
        }
        int trFinIdx = html.indexOf("</tr", trIdx);
        return html.substring(trIdx, trFinIdx);
    }

    private static String getTableRowCellFrom(String html, int colIdx) {
        int tdIdx = 0;
        for (int i = 0; i < colIdx + 1; i++) {
            tdIdx = html.indexOf("<td", tdIdx + 1);
        }
        int tdFinIdx = html.indexOf("</td", tdIdx);
        return html.substring(tdIdx, tdFinIdx);
    }

    private static String getValueBeetweenFont(String html) {
        int fontIniIdx = html.indexOf("<font");
        int fontFinIdx = html.indexOf(">", fontIniIdx);
        int fontCloseIdx = html.indexOf("</font", fontFinIdx);
        String value = html.substring(fontFinIdx + 1, fontCloseIdx).trim();
        return value.replaceAll("<br>", " ").replaceAll("&nbsp;", " ");
    }
}

